package cn.anc.aonicardv.manager;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Build;

import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.MyApplication;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.bean.FirmwareBean;
import cn.anc.aonicardv.bean.ResponseResultBean;
import cn.anc.aonicardv.net.ApiManager;
import cn.anc.aonicardv.net.NetRequestClient;
import cn.anc.aonicardv.net.ProgressSubscriber;
import cn.anc.aonicardv.param.ParamManager;
import cn.anc.aonicardv.util.DateUtils;
import cn.anc.aonicardv.util.LogUtil;
import cn.anc.aonicardv.util.NetUtils;
import cn.anc.aonicardv.widget.FirmwareUpdateDialog;
import cn.anc.httpcontrolutil.SocketUtils;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * function :
 * </p>
 */
public class CheckUpdatesVersionManager {
    private Activity context = null;
    private NetRequestClient netRequestClient;
    private ApiManager apiManager;
    private String mModel;
    private String mTime;
    private  static  String TAG = "llc_CheckUpdatesVersionManager";
    public CheckUpdatesVersionManager(Activity context) {
        this.context = context;
    }
    public void checkUpdate(String model, String buildTime) {
        if (null==model||null==buildTime){
            LogUtil.e(TAG,"-----------checkUpdate---model---buildTime-:不能为空");
            return;
        }
        mModel = model;
        mTime = buildTime;
        NetRequestClient.Builder builder = new NetRequestClient.Builder();
        netRequestClient = builder.addConverterFactory(GsonConverterFactory.create()).baseUrl(ParamManager.CarServerParam.http_base_url).build();
        apiManager = netRequestClient.getRetrofit().create(ApiManager.class);


        checkFirmwareUpdate();
    }
    private FirmwareBean firmwareBean;

    private void checkFirmwareUpdate() {
        netRequestClient.execute2(apiManager.checkFirmwareUpdate(Constant.NetParam.BASE_URL + "static/default/ota/" + mModel + "/config.json"), new ProgressSubscriber(context) {

            @Override
            public void onStart() {
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onCancel() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(ResponseResultBean resultBean) {
                firmwareBean = (FirmwareBean) resultBean;
                if (null!=firmwareBean){
                    long currentTime = DateUtils.getDate(mTime, true, true, true, true, DateUtils.FORMAT_ONE).getTime();
                    long serverTime = DateUtils.getDate(firmwareBean.getBuild_time(), true, true, true, true, DateUtils.FORMAT_ONE).getTime();

                    if (firmwareBean.getModel().equals(mModel) && serverTime > currentTime) {
                        showUpdateDialog();

                    }
                }

            }
        });
    }
    private FirmwareUpdateDialog firmwareUpdateDialog;

    public void dismissUpdateDialog() {
            if (null!=firmwareUpdateDialog){
                if (firmwareUpdateDialog.isShowing()){
                    firmwareUpdateDialog.dismiss();
                }
            }
    }
    private int mDialogId = 10000;
    private void showUpdateDialog() {
        mDialogId ++;
        firmwareUpdateDialog = new FirmwareUpdateDialog(context, R.style.BaseDialogStyle, mModel, firmwareBean.getFirmware_name(), firmwareBean.getFirmware_md5(),mDialogId);
        firmwareUpdateDialog.show();
        firmwareUpdateDialog.setUpdateContent(firmwareBean.getUpgrade_log());
        firmwareUpdateDialog.setVersionCode(firmwareBean.getVersion());
    }

}
